﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:t_user
    /// </summary>
    public partial class t_user
    {
        public t_user()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("t_userid", "t_user");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from t_user");
            strSql.Append(" where t_userid=@t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_userid", SqlDbType.Int, 4)
            };
            parameters[0].Value = t_userid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.t_user model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into t_user(");
            strSql.Append("t_name,t_pwd,t_lastdate,t_count,t_remark,Deleted,EtypeFID,isSystem)");
            strSql.Append(" values (");
            strSql.Append("@t_name,@t_pwd,@t_lastdate,@t_count,@t_remark,@Deleted,@EtypeFID,@isSystem)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@t_name", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_pwd", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_lastdate", SqlDbType.DateTime),
                    new SqlParameter("@t_count", SqlDbType.Int, 4),
                    new SqlParameter("@t_remark", SqlDbType.VarChar, 100),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@EtypeFID", SqlDbType.Int, 4),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4) };
            parameters[0].Value = model.t_name;
            parameters[1].Value = model.t_pwd;
            parameters[2].Value = model.t_lastdate;
            parameters[3].Value = model.t_count;
            parameters[4].Value = model.t_remark;
            parameters[5].Value = model.Deleted;
            parameters[6].Value = model.EtypeFID;
            parameters[7].Value = model.isSystem;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.t_user model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update t_user set ");
            strSql.Append("t_name=@t_name,");
            strSql.Append("t_pwd=@t_pwd,");
            strSql.Append("t_lastdate=@t_lastdate,");
            strSql.Append("t_count=@t_count,");
            strSql.Append("t_remark=@t_remark,");
            strSql.Append("Deleted=@Deleted,");
            strSql.Append("EtypeFID=@EtypeFID,");
            strSql.Append("isSystem=@isSystem");
            strSql.Append(" where t_userid=@t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_name", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_pwd", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_lastdate", SqlDbType.DateTime),
                    new SqlParameter("@t_count", SqlDbType.Int, 4),
                    new SqlParameter("@t_remark", SqlDbType.VarChar, 100),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@EtypeFID", SqlDbType.Int, 4),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4),
                    new SqlParameter("@t_userid", SqlDbType.Int, 4) };
            parameters[0].Value = model.t_name;
            parameters[1].Value = model.t_pwd;
            parameters[2].Value = model.t_lastdate;
            parameters[3].Value = model.t_count;
            parameters[4].Value = model.t_remark;
            parameters[5].Value = model.Deleted;
            parameters[6].Value = model.EtypeFID;
            parameters[7].Value = model.isSystem;
            parameters[8].Value = model.t_userid;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from t_user ");
            strSql.Append(" where t_userid=@t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_userid", SqlDbType.Int, 4)
            };
            parameters[0].Value = t_userid;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string t_useridlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from t_user ");
            strSql.Append(" where t_userid in (" + t_useridlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.t_user GetModel(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 t_userid,t_name,t_pwd,t_lastdate,t_count,t_remark,Deleted,EtypeFID,isSystem from t_user ");
            strSql.Append(" where t_userid=@t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_userid", SqlDbType.Int, 4)
            };
            parameters[0].Value = t_userid;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.t_user DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.t_user();
            if (row != null)
            {
                if (row["t_userid"] != null && row["t_userid"].ToString() != string.Empty)
                {
                    model.t_userid = int.Parse(row["t_userid"].ToString());
                }
                if (row["t_name"] != null)
                {
                    model.t_name = row["t_name"].ToString();
                }
                if (row["t_pwd"] != null)
                {
                    model.t_pwd = row["t_pwd"].ToString();
                }
                if (row["t_lastdate"] != null && row["t_lastdate"].ToString() != string.Empty)
                {
                    model.t_lastdate = DateTime.Parse(row["t_lastdate"].ToString());
                }
                if (row["t_count"] != null && row["t_count"].ToString() != string.Empty)
                {
                    model.t_count = int.Parse(row["t_count"].ToString());
                }
                if (row["t_remark"] != null)
                {
                    model.t_remark = row["t_remark"].ToString();
                }
                if (row["Deleted"] != null && row["Deleted"].ToString() != string.Empty)
                {
                    if ((row["Deleted"].ToString() == "1") || (row["Deleted"].ToString().ToLower() == "true"))
                    {
                        model.Deleted = true;
                    }
                    else
                    {
                        model.Deleted = false;
                    }
                }
                if (row["EtypeFID"] != null && row["EtypeFID"].ToString() != string.Empty)
                {
                    model.EtypeFID = int.Parse(row["EtypeFID"].ToString());
                }
                if (row["isSystem"] != null && row["isSystem"].ToString() != string.Empty)
                {
                    model.isSystem = int.Parse(row["isSystem"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select t_userid,t_name,t_pwd,t_lastdate,t_count,t_remark,Deleted,EtypeFID,isSystem ");
            strSql.Append(" FROM t_user ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" t_userid,t_name,t_pwd,t_lastdate,t_count,t_remark,Deleted,EtypeFID,isSystem ");
            strSql.Append(" FROM t_user ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_user ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.t_userid desc");
            }
            strSql.Append(")AS Row, T.*  from t_user T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取数据行-扩展
        /// </summary>
        /// <param name="t_userid"></param>
        /// <returns></returns>
        public DataTable GetRecordByID(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select top 1 u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, ");
            strSql.Append("e.Name EmployeeName, e.Code EmployeeCode from t_user u ");
            strSql.Append("left join Employee e on e.ID=u.EtypeFID ");
            strSql.Append(" where t_userid=@t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_userid", SqlDbType.Int, 4)
            };
            parameters[0].Value = t_userid;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 增加一条数据
        /// 返回-1001:用户名重复
        /// 返回-1002:关联用户不存在
        /// </summary>
        public int AddExpand(DMZS.Model.t_user model)
        {
            var strSql = new StringBuilder();
            strSql.Append("declare @nameCount int, @employeeCount int  ");
            strSql.Append("select @nameCount=count(1) from t_user where t_name=@t_name ");
            strSql.Append("select @employeeCount=count(1) from Employee where ID=@EtypeFID ");
            strSql.Append("if @nameCount>0 begin select -1001 return end ");
            strSql.Append("if (@EtypeFID<>0) and (@employeeCount=0) begin select -1002 return end ");
            strSql.Append("insert into t_user(");
            strSql.Append("t_name,t_pwd,t_lastdate,t_count,t_remark,Deleted,EtypeFID,isSystem) ");
            strSql.Append("values (");
            strSql.Append("@t_name,@t_pwd,@t_lastdate,@t_count,@t_remark,@Deleted,@EtypeFID,@isSystem); ");
            strSql.Append("select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@t_name", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_pwd", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_lastdate", SqlDbType.DateTime),
                    new SqlParameter("@t_count", SqlDbType.Int, 4),
                    new SqlParameter("@t_remark", SqlDbType.VarChar, 100),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@EtypeFID", SqlDbType.Int, 4),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4) };
            parameters[0].Value = model.t_name;
            parameters[1].Value = model.t_pwd;
            parameters[2].Value = model.t_lastdate;
            parameters[3].Value = model.t_count;
            parameters[4].Value = model.t_remark;
            parameters[5].Value = model.Deleted;
            parameters[6].Value = model.EtypeFID;
            parameters[7].Value = model.isSystem;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// 返回-1001:用户名重复
        /// 返回-1002:关联用户不存在
        /// </summary>
        public int UpdateExpand(DMZS.Model.t_user model)
        {
            var strSql = new StringBuilder();
            strSql.Append("declare @nameCount int, @employeeCount int  ");
            strSql.Append("select @nameCount=count(1) from t_user where t_name=@t_name and t_userid<>@t_userid ");
            strSql.Append("select @employeeCount=count(1) from Employee where ID=@EtypeFID ");
            strSql.Append("if @nameCount>0 begin select -1001 return end ");
            strSql.Append("if (@EtypeFID<>0) and (@employeeCount=0) begin select -1002 return end ");
            strSql.Append("update t_user set ");
            strSql.Append("t_name=@t_name,");
            strSql.Append("t_pwd=@t_pwd,");
            //strSql.Append("t_lastdate=@t_lastdate,");
            strSql.Append("t_count=@t_count,");
            strSql.Append("t_remark=@t_remark,");
            strSql.Append("Deleted=@Deleted,");
            strSql.Append("EtypeFID=@EtypeFID,");
            strSql.Append("isSystem=@isSystem");
            strSql.Append(" where t_userid=@t_userid ");
            strSql.Append("select @t_userid");
            var parameters = new SqlParameter[] { new SqlParameter("@t_name", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_pwd", SqlDbType.VarChar, 50),
                    new SqlParameter("@t_lastdate", SqlDbType.DateTime),
                    new SqlParameter("@t_count", SqlDbType.Int, 4),
                    new SqlParameter("@t_remark", SqlDbType.VarChar, 100),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@EtypeFID", SqlDbType.Int, 4),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4),
                    new SqlParameter("@t_userid", SqlDbType.Int, 4) };
            parameters[0].Value = model.t_name;
            parameters[1].Value = model.t_pwd;
            parameters[2].Value = model.t_lastdate;
            parameters[3].Value = model.t_count;
            parameters[4].Value = model.t_remark;
            parameters[5].Value = model.Deleted;
            parameters[6].Value = model.EtypeFID;
            parameters[7].Value = model.isSystem;
            parameters[8].Value = model.t_userid;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListForUIExpand(string strWhere)
        {
            var strSql = @"
select u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, 
    e.Name EmployeeName, e.Code EmployeeCode 
from t_user u 
left join Employee e on e.ID=u.EtypeFID 
where u.Deleted=0 ";
            if (strWhere.Trim() != string.Empty)
            {
                strSql += " where " + strWhere;
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 第一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetFirst()
        {
            var strSql = new StringBuilder();
            strSql.Append("select top 1 u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, ");
            strSql.Append("e.Name EmployeeName, e.Code EmployeeCode from t_user u ");
            strSql.Append("left join Employee e on e.ID=u.EtypeFID ");
            strSql.Append("where u.Deleted=0 ");

            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 最后一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetLast()
        {
            var strSql = new StringBuilder();
            strSql.Append("select top 1 u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, ");
            strSql.Append("e.Name EmployeeName, e.Code EmployeeCode from t_user u ");
            strSql.Append("left join Employee e on e.ID=u.EtypeFID ");
            strSql.Append("where u.Deleted=0 ");
            strSql.Append("order by t_userid desc ");

            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 上一条记录
        /// </summary>
        /// <param name="t_userid"></param>
        /// <returns></returns>
        public DataRow GetPrevious(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select top 1 u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, ");
            strSql.Append("e.Name EmployeeName, e.Code EmployeeCode from t_user u ");
            strSql.Append("left join Employee e on e.ID=u.EtypeFID ");
            strSql.Append("where u.Deleted=0 and u.t_userid<" + t_userid.ToString());
            strSql.Append(" order by t_userid desc ");

            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }
        /// <summary>
        /// 下一条记录
        /// </summary>
        /// <param name="t_userid"></param>
        /// <returns></returns>
        public DataRow GetNext(int t_userid)
        {
            var strSql = new StringBuilder();
            strSql.Append("select top 1 u.t_userid,u.t_name,u.t_pwd,u.t_lastdate,u.t_count,u.t_remark,u.Deleted,u.EtypeFID,u.isSystem, ");
            strSql.Append("e.Name EmployeeName, e.Code EmployeeCode from t_user u ");
            strSql.Append("left join Employee e on e.ID=u.EtypeFID ");
            strSql.Append("where u.Deleted=0 and u.t_userid>" + t_userid.ToString());
            strSql.Append(" order by t_userid  ");

            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }
    }
}

